﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class caomeo_control_YKienKhachHang : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            bindGridPaging();
        }
    }
    public void bindGridPaging()
    {
        YKienKhachHangDAO yKien = new YKienKhachHangDAO();
        PagingOpbject obj = yKien.getForDisplay_Paging(1, pager1.PageSize, pager1.CurrentIndex);
        rptPhanHoi.DataSource = obj.tableReturn;
        rptPhanHoi.DataBind();
        pager1.ItemCount = obj.totalRecords;
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        pager1.CurrentIndex = currnetPageIndx;
        bindGridPaging();
    }
    protected void cmdCapNhat_Click(object sender, EventArgs e)
    {
        lblErr.Text = "";
        lblOk.Text = "";
        //Kiem loi
        if (string.IsNullOrEmpty(txtHoTen.Text))
        {
            lblErr.Text = "Bạn vui lòng nhập họ tên!";
            MyWebUtils.MyWebUtil.goToElement(this.Page, txtHoTen.ClientID);
            return;
        }
        if (string.IsNullOrEmpty(txtDiaChi.Text))
        {
            lblErr.Text = "Bạn vui lòng nhập địa chỉ!";
            MyWebUtils.MyWebUtil.goToElement(this.Page, txtDiaChi.ClientID);
            return;
        }
        if (string.IsNullOrEmpty(txtNoiDung.Text))
        {
            lblErr.Text = "Bạn vui lòng nhập nội dung!";
            MyWebUtils.MyWebUtil.goToElement(this.Page, txtNoiDung.ClientID);
            return;
        }
        // xong kiem loi
        string daGui = TTQT.TTQT_Code.TTQTCommon.ToString(ViewState["gui"]);
        if(daGui.Equals("1"))
        {
            Response.Redirect("Default.aspx");
            return;
        }
        YKienKhachHangDAO ykien =new YKienKhachHangDAO();
        ykien.FullName = txtHoTen.Text;
        ykien.Email = txtEmail.Text;
        ykien.address = txtDiaChi.Text;
        ykien.detail = txtNoiDung.Text;
        ykien.IP = Request.UserHostAddress;
        int kq=ykien.insert();
        if(kq>0)
        {
            lblOk.Text = "Cảm ơn bạn đã đóng góp ý kiến!";
            ViewState["gui"] = "1";
            //Gui email
            SMTP smtp =new SMTP();
            smtp.SendEmailUsingGmail("kiennnit@gmail.com", "Caomeo.com - Ý kiến từ "+txtHoTen.Text,
                                     ykien.detail);
        }
    }
    protected void txtReset_Click(object sender, EventArgs e)
    {
        txtDiaChi.Text = string.Empty;
        txtEmail.Text = string.Empty;
        txtHoTen.Text = string.Empty;
        txtNoiDung.Text = string.Empty;
    }
}
